package com.zs.studentmanager.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author: 周小白
 * @date: 2024-01-19 21:30
 **/
@Configuration
public class WebMVCConfiguration implements WebMvcConfigurer {

    @Value("${user.icon}")
    private String path;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String url = path.replaceAll("\\\\", "/");
        registry.addResourceHandler("/upload/**")
                .addResourceLocations(url)
                .setCachePeriod(3600);
    }
}
